package com.heimo.agentlicence.ui.fragment;

import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.heimo.agentlicence.R;
import com.heimo.agentlicence.http.HttpHelper;
import com.heimo.agentlicence.task.BasicTask;
import com.heimo.agentlicence.task.DisciplineCheckTask;
import com.heimo.agentlicence.task.DisciplineLoadTask;
import com.heimo.agentlicence.task.OnTaskCompleted;
import com.heimo.agentlicence.ui.DialogHelper;
import com.heimo.agentlicence.ui.adaptor.ScreenSlidePagerAdapter;

public class CheckDisciplineFragment extends BasicFragment {
	private final String TAG = "CheckDisciplineFragment";
	private List<NameValuePair> nvps;
	private String checkedLicenceNo = null;
	
	public static CheckDisciplineFragment newInstance(Context ctx,
			ScreenSlidePagerAdapter adp) {
		CheckDisciplineFragment f = new CheckDisciplineFragment();
		f.setContext(ctx);
		f.setAdapter(adp);
		return f;
	}

	private void init() {
		TextView tx = (TextView) v.findViewById(R.id.titleDiscipline);
		Typeface tf = Typeface.createFromAsset(ctx.getAssets(),
				"fonts/orbitron-bold.otf");
		tx.setTypeface(tf);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		v = (ViewGroup) inflater.inflate(R.layout.activity_check_discipline,
				container, false);
		init();
		return v;
	}
	
	@Override
	public void updateLicence(String licenceNo){
		this.licenceNo = licenceNo;
		TextView tx = (TextView) v.findViewById(R.id.licenceNo);
		if(licenceNo!=null){
			tx.setText(licenceNo);			
		}else{
			tx.setText("");
		}
	}
	
	public void fnPageLoad(){
		if(this.licenceNo==null){
			return;
		}
		if(checkedLicenceNo!= this.licenceNo){
			checkedLicenceNo = this.licenceNo;
			Log.d(TAG, "fnLoadPage() - Start");
			fnLoadPage();			
		}
	}
	
	
	private void fnLoadPage() {
		OnTaskCompleted completeAction = new OnTaskCompleted() {
			public void onTaskCompleted(Object s) {
				nvps = (List<NameValuePair>) s;
				fnSetLicenceNo();
				fnSendCheckRequest();
			}


			public void onTaskFailed(String errMessage) {
				Log.d(TAG, errMessage);
				DialogHelper.getOkAlertDialog(ctx, "Error Fail", errMessage)
						.show();
			}
		};
		DisciplineLoadTask llt = new DisciplineLoadTask(ctx, completeAction);
		llt.fnLoadPage();
	}
	
	private void fnSetLicenceNo() {
		if (nvps != null) {
			String tempName = "";
			for (int i = 0; i < nvps.size(); i++) {
				NameValuePair nvp = nvps.get(i);
				if (nvp.getName().contains("LicenceNo")) {
					tempName = nvp.getName();
					nvps.remove(i);
				}
			}
			nvps.add(new BasicNameValuePair(tempName, licenceNo));
		}
	}
	
	private void fnSendCheckRequest() {
		OnTaskCompleted completeAction = new OnTaskCompleted() {
			public void onTaskCompleted(Object s) {
				boolean hasRecord = (Boolean)s;
				TextView tx = (TextView) v.findViewById(R.id.licenceNo);
				if(hasRecord){
					String urlString = HttpHelper.URL_BASIC+BasicTask.PATH_LICENCE_LOAD_PAGE;
					tx.setText("Discipline record has been found!\nPlease follow the link below for more details:\n"+urlString);
				}else{
					tx.setText("No discipline record has been found for "+licenceNo);
				}
			}

			public void onTaskFailed(String errMessage) {
				Log.d(TAG, errMessage);
				DialogHelper.getOkAlertDialog(ctx, "Error Fail", errMessage)
						.show();
			}
		};
		DisciplineCheckTask llt = new DisciplineCheckTask(ctx, completeAction);
		llt.fnLoadPage(nvps);
	}
}
